#ifndef IMAGEPART_H
#define IMAGEPART_H

#include <QGraphicsWidget>
#include <QRect>

class ImagePart : public QGraphicsWidget
{

    Q_OBJECT

private:
    bool m_isValid;
    QRect m_rect;
    int m_role;

    int m_order;
    int m_roleMax;
    QColor m_color;
    QPointF m_diff;


    QColor ipColor ();



public:
    ImagePart (const QRect& r, const int role = Green, int const order = 1, QGraphicsItem* parent = 0);

    enum { Green = 0, Red = 1 };

    inline static qreal borderSize () { return 8.0; }
    void clear () {
        setValid (false);
        setGreen ();
    }

    bool isValid () const { return m_isValid; }
    void setValid (const bool b) { m_isValid = b; }

    void setGreen () { m_role = Green; }
    void setRed () { m_role = Red; }

    bool isGreen () const { return role () == Green; }
    bool isRed () const { return role () == Red; }

    bool operator==(const ImagePart& other) const {
        return isValid () and other.isValid ()
                and m_rect == other.m_rect
                and m_role == other.m_role; }

    bool operator!= (const ImagePart& other) const { return not operator== (other); }

    void mousePressEvent (QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent (QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
    void hoverEnterEvent (QGraphicsSceneHoverEvent *);

    void paintWindowFrame (QPainter * /*painter*/,
                           const QStyleOptionGraphicsItem * /*option*/,
                           QWidget * /*widget*/ = 0 );

    void paint (QPainter *painter,
                const QStyleOptionGraphicsItem *option,
                QWidget *widget);

    void setRectangle (const QRect &r) { m_rect = r ; setGeometry (r); }
    QRect rectangle () const { return geometry ().toRect (); }
    QRectF boundingRectToScene ();
    QRectF clientRectToScene ();

    int order () const { if (isGreen ()) return m_order; return -1; }
    int role () const { return m_role; }

    void setOrder (const int o)
    {
       if (not isGreen () or m_order == o) return;

       m_order = o;
        update ();
    }

    void setRole (const int r)
    {
        if (r == m_role or r < 0 or r > m_roleMax) return;

        m_role = r;
        update ();
    }

    void setNextRole (const int order)
    {
        const int r = (role () + 1) % (m_roleMax + 1);
        m_role = r;
        if (r == 0) m_order = order;
        update ();
    }

    void setRoleMax (const int max) { m_roleMax = max; }

//    static QList<SelectRect> partsToSelectRects (const QList<ImagePart *> &list);
    static QVector<QRectF> partsToRects (const QList<ImagePart *> &list);

signals:
};

#endif // IMAGEPART_H
